package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2022/07/12 16:15
 */
@RestController
public class ArticleController {

    //@RequestMapping("qq_53847859/article/details/125734402")
    //@RequestMapping("*/article/details/*")
    //public BaseRespVo articleDetails(HttpServletRequest request) {
    //    String requestURI = request.getRequestURI();
    //    // 对URI这个字符串做截取，可以获得第一个通配符和最后一个通配符位置的值
    //    // 但是这个过程需要程序开发人员自己来做
    //    return BaseRespVo.ok(null);
    //}

    // {}里的值作为占位符
    // 通过@PathVariable注解的value属性 获得请求URL中的指定占位符的值，给该注解对应的形参赋值
    @RequestMapping("{username}/article/details/{id}")
    public BaseRespVo articleDetails(@PathVariable("username") String username,
                                     @PathVariable("id") Integer id) {
        return BaseRespVo.ok(null);
    }

    /*@RequestMapping("csdnnews/article/details/125734414")
    public BaseRespVo articleDetails2(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        return BaseRespVo.ok(null);
    }*/

    // 会限定你必须要携带对应的请求参数
    // 我们基本上不会用，我们通过请求参数名和Handler方法的形参名一致这种方式来接收
    //localhost:8080/login?username=songge&password=niupi
    @RequestMapping("login")
    public BaseRespVo login(@RequestParam("username") String data1,
                            @RequestParam("password") String data2) {
        return BaseRespVo.ok(null);
    }

    //通过@RequestHeader注解的value属性 获得指定请求头的值，赋值给该注解对应的形参
    // 形参可以使用字符串来接收，也可以使用字符串数组来接收，
    // 如果用数组来接收，其实做的事情就是将字符串根据逗号转换数组
    @RequestMapping("header")
    /*public BaseRespVo header(HttpServletRequest request) {
        String accept = request.getHeader("Accept");
        String host = request.getHeader("Host");*/
    //public BaseRespVo header(@RequestHeader("Accept") String accept,
    public BaseRespVo header(@RequestHeader("Accept") String[] accept,
                             @RequestHeader("Host") String host) {

        return BaseRespVo.ok(null);
    }

    // @CookieValue
    //通过@CookieValue注解的value属性 获得指定Cookie的值，赋值给该注解对应的形参
    // 形参类型是 String
    @RequestMapping("cookie")
    /*public BaseRespVo cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String value = null;
        for (Cookie cookie : cookies) {
            if ("songge".equals(cookie.getName())) {
                value = cookie.getValue();
            }
        }*/
    public BaseRespVo cookie(@CookieValue("songge") String value){
        return BaseRespVo.ok(null);
    }

    // @SessionAttribute
    // 通过@SessionAttribute注解的value属性 获得指定Session的值，赋值给该注解对应的形参
    // 形参的类型是 Object
    @RequestMapping("session")
    /*public BaseRespVo session(HttpSession session) {
        Object username = session.getAttribute("username");*/
    public BaseRespVo session(@SessionAttribute("username") String value){
        return BaseRespVo.ok(null);
    }

    @RequestMapping("session/put/{value}")
    public BaseRespVo sessionPut(@PathVariable("value") String value,HttpSession session) {
        session.setAttribute("username", value);
        return BaseRespVo.ok(value);
    }
}
